package com.qingzhu.controller.goodsserver;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.Goods;
import com.qingzhu.pojo.Item;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.GoodsService;
import com.qingzhu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/goods/update")
public class AdminUpdateGoodsController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    ObjectMapper mapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("修改商品信息..");

        String server = req.getParameter("server");
        if (server.equals("item")) {
            updateItem(req,resp);
        }else if (server.equals("goods")) {
            updateGoods(req,resp);
        }else if (server.equals("itemState")) {
            updateItemState(req,resp);
        }else if (server.equals("goodsState")) {
            updateGoodsState(req,resp);
        }else if (server.equals("delAll")) {
            updateItemDelStateAll(req,resp);
        }else {
            resp.getWriter().write(mapper.writeValueAsString(new ResultVo(1001,"服务器标志为空")));
        }
    }


    // 修改单品（SKU）信息
    public void updateItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String itemJson = req.getReader().readLine();
        Item item = mapper.readValue(itemJson, Item.class);
        goodsService.updateItem(item);
        resp.getWriter().write(mapper.writeValueAsString(new ResultVo(200,"修改成功")));
    }
    // 修改商品（SPU）信息
    public void updateGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String goodsJson = req.getReader().readLine();
        Goods goods = mapper.readValue(goodsJson, Goods.class);
        goodsService.updateGoods(goods);
        resp.getWriter().write(mapper.writeValueAsString(new ResultVo(200,"修改成功")));
    }
    // 修改商品（SKU）的状态
    public void updateItemState(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String state = req.getParameter("state");
        String itemId = req.getParameter("itemId");
        goodsService.updateItemState(state,itemId);
        resp.getWriter().write(mapper.writeValueAsString(new ResultVo(200,"修改成功")));
    }
    // 修改商品（SPU）的状态
    public void updateGoodsState(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String state = req.getParameter("state");
        String goodsId = req.getParameter("goodsId");
        goodsService.updateGoodsState(state, goodsId);
        resp.getWriter().write(mapper.writeValueAsString(new ResultVo(200,"修改成功")));
    }
    // 修改所有选中商品的状态
    public void updateItemDelStateAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String state = req.getParameter("state");
        String itemIdStr = req.getReader().readLine();
        List<String> itemIds = mapper.readValue(itemIdStr, new TypeReference<List<String>>() {});
        goodsService.updateItemDelStateAll(state,itemIds);
        resp.getWriter().write(mapper.writeValueAsString(new ResultVo(200,"修改成功")));
    }
}
